package com.frxuexi.user.view

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import com.frxuexi.lib_base.IUiState
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.lib_base.room.AskLeave
import com.frxuexi.user.R
import com.frxuexi.user.intent.AskIntent
import com.frxuexi.user.viewmodel.AskViewModel
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

//请假列表页面
@Composable
fun AskListPage(navHostController: NavHostController,vm:AskViewModel= hiltViewModel()) {
   /* var list = remember {
        SnapshotStateList<AskLeave>()
    }*/



    val uid = MMKV.defaultMMKV().decodeInt("uid", 0)
    val list = vm.getData(uid).collectAsLazyPagingItems()
    /*LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect{
                when(it){
                    is IUiState.onSuccess<*> ->{
                        list.addAll(it.data as Collection<AskLeave>)
                    }
                    is IUiState.onError->{
                        Log.i("===",it.msg)
                    }
                    else -> {}
                }
            }
        }

        vm.sendIntent(AskIntent.askListByUid(uid))
    })*/

    Column(Modifier.padding(20.dp)) {
        Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
            Image(
                imageVector = Icons.Default.ArrowBack,
                contentDescription = "",
                Modifier.clickable {
                    navHostController.popBackStack()
                })
            Text(text = "申请列表")
            Text(text = "申请", Modifier.clickable {
                navHostController.navigate(RouterPath.ASKFOR_PAGE)
            })
        }
        Spacer(modifier = Modifier.height(10.dp))
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(vertical = 16.dp),
            horizontalArrangement = Arrangement.SpaceAround
        ) {
            Text(text = "类型")
            Text(text = "开始时间")
            Text(text = "结束时间")
            Text(text = "审批状态")
        }
        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn(verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .background(
                            colorResource(id = R.color.lightgray),
                            RoundedCornerShape(20.dp)
                        )
                        .padding(vertical = 16.dp)
                        .clickable {
                            val json = Gson().toJson(it)
                            navHostController.navigate(RouterPath.USERASKDETAIL_PAGE + "/$json")
                        },
                    horizontalArrangement = Arrangement.SpaceAround,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(text = it?.type!!)
                    Text(text = it.start!!)
                    Text(text = it.end!!)
                    var color=if(it.status=="已提交") Color.LightGray else if(it.status=="同意") Color.Green else Color.Red
                    Text(text = it.status!!,
                        Modifier
                            .background(color, RoundedCornerShape(10.dp))
                            .padding(5.dp))
                }
            }
        })
    }
}